Test Setup Failed
Pull Request — master (#19)
by Flo
03:33
created

faulancer.namespace   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 26
rs 8.5806
c 1
b 0
f 1
cc 4
nc 6
nop 1
1
/**
2
 * Define namespaces in javascript
3
 *
4
 * Usage:
5
 * _________________________________________________________________
6
 *|
7
 *| (function(dependency1, dependency2) {
8
 *|
9
 *|   'use strict';
10
 *|
11
 *|   // The namespace
12
 *|   var ns = faulancer.namespace('app.myscript');
13
 *|
14
 *|   // Private scope
15
 *|   var _private = {
16
 *|
17
 *|     doPrivateThings: function() {
18
 *|       this won't get exposed in public scope...
19
 *|     }
20
 *|
21
 *|   };
22
 *|
23
 *|   // Helper methods
24
 *|   var _helper = {
25
 *|     define helper methods...
26
 *|   };
27
 *|
28
 *|   // Private fields/attributes (with example values)
29
 *|   var _fields = {
30
 *|     fromTop:    0,
31
 *|     fromLeft:   0
32
 *|   };
33
 *|
34
 *|   // Public scope
35
 *|   ns = {
36
 *|
37
 *|     init: function() {
38
 *|       do initialization...
39
 *|     },
40
 *|
41
 *|     doThis: function() {
42
 *|       do this...
43
 *|     },
44
 *|
45
 *|     doThat: function() {
46
 *|       and that...
47
 *|     }
48
 *|
49
 *|   };
50
 *|
51
 *|   window.addEventListener('load', faulancer.app.myscript.init);
52
 *|
53
 *| }(faulancer.app.dependency1, faulancer.app.dependency2);
54
 *|_________________________________________________________________
55
 *
56
 */
57
58
59
if (typeof faulancer === 'undefined') {
0 ignored issues
show
Bug introduced by
The variable faulancer seems to be never declared. If this is a global, consider adding a /** global: faulancer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
60
    faulancer = {};
0 ignored issues
show
Bug introduced by
The variable faulancer seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.faulancer.
Loading history...
61
}
62
63
faulancer.namespace = function(namespace)
0 ignored issues
show
Bug introduced by
The variable faulancer does not seem to be initialized in case typeof faulancer === "undefined" on line 59 is false. Are you sure this can never be the case?
Loading history...
64
{
65
    'use strict';
66
67
    var parts = namespace.split('.'),
68
        parent = faulancer,
0 ignored issues
show
Bug introduced by
The variable faulancer does not seem to be initialized in case typeof faulancer === "undefined" on line 59 is false. Are you sure this can never be the case?
Loading history...
69
        i;
70
71
    // strip redundant leading global
72
    if (parts[0] === "faulancer") {
73
        parts = parts.slice(1);
74
    }
75
76
    for (i = 0; i < parts.length; i += 1) {
77
78
        // create a property if it doesn't exist
79
        if (typeof parent[parts[i]] === "undefined") {
80
            parent[parts[i]] = {};
81
        }
82
83
        parent = parent[parts[i]];
84
85
    }
86
87
    return parent;
88
};
89